#include"TcpServer.hpp"
#include"Httpserver.hpp"

using namespace std;

void usage(string proc)
{
    cout << "Usage:\n\t" << proc << " local_port\n"
         << endl;
}
shared_ptr<HttpResponse> login(shared_ptr<HttpRequest> req)
{
     std::string userdata;
    if (req->Method() == "GET")
    {
        userdata = req->Args();
    }
    else if (req->Method() == "POST")
    {
        userdata = req->Text();
    }
    // 处理数据了
    Log(DEBUG, "enter data handler, data is : %s", userdata.c_str());
    auto response = Factor::BuildResponse();
    response->AddStauasline(200,"OK");
    response->AddHeader("Content-Type", "text/html");
    response->AddText("<html><h1>maba out!!!</h1></html>");
    Log(DEBUG, "=========================");
    return response;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        usage(argv[0]);
        exit(1);
    }
    uint16_t port = stoi(argv[1]);
    HttpServer httpservice;
    httpservice.AddHandler("/login",login);
    TcpServer tsvr(port,bind(&HttpServer::HttpHander,&httpservice,std::placeholders::_1));
    tsvr.Loop();

    return 0;
}
